package com.supreme.studyserver.threadstore;

import com.supreme.common.constant.ConsoleColors;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Date;

public class ThreadStore {

    @Data
    @Accessors(chain = true)
    public static class Store {
        private String name;
        private Integer clientId;
        private Date date;
    }

    private static final ThreadLocal<Store> threadLocal = ThreadLocal.withInitial(() -> new Store()
            .setName("threadStore")
            .setClientId(0)
            .setDate(new Date()));

    public static void setStore(Integer clientId) {
        final Store threadStore = new Store()
                .setName("threadStore")
                .setClientId(clientId)
                .setDate(new Date());
        threadLocal.set(threadStore);
    }

    public static Store getStore() {
        return threadLocal.get();
    }

    public static void removeStore() {
        threadLocal.remove();
    }

    public static void printStoreInfo() {
        final Store store = threadLocal.get();
        ConsoleColors.println(ConsoleColors.GREEN_BOLD_BRIGHT, "threadStore: " + store);
    }

    public static void printThreadInfo() {
        final Thread currentThread = Thread.currentThread();
        final String threadName = currentThread.getName();
        final long threadId = currentThread.getId();
        ConsoleColors.printf(ConsoleColors.GREEN_BOLD_BRIGHT, "threadName: [%s] threadId: [%s]\n", threadName, threadId);
    }
}
